package cn.wolfcode.web.controller;


import cn.wolfcode.domain.Permission;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IPermissionService;
import cn.wolfcode.util.RequiredPermission;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    private IPermissionService permissionService;


    @RequiredPermission(name="权限页面",expression="permission:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo){
        Page<Permission> pageInfo = permissionService.queryPage(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "permission/list";
    }


    @RequiredPermission(name="权限删除",expression="permission:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
           permissionService.removeById(id);
        }
        return "redirect:/permission/list";
    }


    @RequiredPermission(name="权限新增/编辑",expression="permission:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Permission permission) {
        if (permission.getId() == null) {
            permissionService.save(permission);
        } else {
            permissionService.updateById(permission);
        }
        return "redirect:/permission/list";
    }


    @RequiredPermission(name="权限新增/编辑",expression="permission:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        if (id != null) {
            model.addAttribute("permission",permissionService.getById(id));
        }
        return "permission/input";
    }
}
